package com.zhiyizhen.handheld.component.execute.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseAdapter;
import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseViewHolder;
import com.zhiyizhen.handheld.component.execute.bean.executedetail.ExecuteDetailBean;
import com.zhiyizhen.handheld.databinding.ItemWaizhiBinding;

public class ExecuteDetailWaizhiAdapter extends BaseAdapter<ExecuteDetailBean.RxInfoBean, ExecuteDetailWaizhiAdapter.CarTypeViewHolder> {
    private Context context;

    private ExecuteDetailWaizhiAdapter.OnActionClickListener onActionClickListener;

    public void setOnActionClickListener(ExecuteDetailWaizhiAdapter.OnActionClickListener onActionClickListener) {
        this.onActionClickListener = onActionClickListener;
    }

    public ExecuteDetailWaizhiAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @NonNull
    @Override
    public ExecuteDetailWaizhiAdapter.CarTypeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ExecuteDetailWaizhiAdapter.CarTypeViewHolder(this, ItemWaizhiBinding.inflate(LayoutInflater.from(parent.getContext())));
    }

    public class CarTypeViewHolder extends BaseViewHolder<ExecuteDetailBean.RxInfoBean> {

        private final ItemWaizhiBinding binding;

        public CarTypeViewHolder(BaseAdapter<ExecuteDetailBean.RxInfoBean, ExecuteDetailWaizhiAdapter.CarTypeViewHolder> adapter, ItemWaizhiBinding viewBinding) {
            super(adapter, viewBinding.getRoot());
            binding = viewBinding;
        }

        @SuppressLint("DefaultLocale")
        @Override
        public void bindData(ExecuteDetailBean.RxInfoBean data) {
            if (data.getExtInfo() != null) {
                binding.groupNameTv.setText(data.getExtInfo().getProjectName());
                String result;
                if (TextUtils.equals(data.getExtInfo().getExecStatus(), "1")) {
                    result = String.format("第%1$s次/共%2$s次", data.getExtInfo().getNextTimes(), data.getExtInfo().getTotalTimes());
                    binding.timesTv.setText(result);
                    binding.dividerExecuteBtn.setVisibility(View.VISIBLE);
                    binding.executeBtn.setVisibility(View.VISIBLE);
                } else {
                    result = String.format("共%1$d次", data.getExtInfo().getTotalTimes());
                    binding.timesTv.setText(result);
                    binding.dividerExecuteBtn.setVisibility(View.GONE);
                    binding.executeBtn.setVisibility(View.GONE);
                }
                binding.frequencyCodeTv.setText(data.getExtInfo().getFrequencyCodeText());
                binding.remarkTv.setText(data.getExtInfo().getRemark());

                if (data.getExtInfo().getDetailList() != null) {
                    GridLayoutManager layoutManager = new GridLayoutManager(context, 5);
                    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    binding.recyclerView.setLayoutManager(layoutManager);
                    ExecuteWaizhiGroupAdapter adapter = new ExecuteWaizhiGroupAdapter(context);
                    adapter.setData(data.getExtInfo().getDetailList());
                    binding.recyclerView.setAdapter(adapter);

                }
            }
            binding.executeBtn.setOnClickListener(v -> {
                if (onActionClickListener != null) {
                    onActionClickListener.onAddExecuteRecord(data);
                }
            });
            if (TextUtils.equals(data.getExtInfo().getExecStatus(), "1")) {
                binding.dividerExecuteBtn.setVisibility(View.VISIBLE);
                binding.executeBtn.setVisibility(View.VISIBLE);
            } else {
                binding.dividerExecuteBtn.setVisibility(View.GONE);
                binding.executeBtn.setVisibility(View.GONE);
            }
            binding.contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        }
    }
    public interface OnActionClickListener {
        /**
         * 添加执行记录
         */
        void onAddExecuteRecord(ExecuteDetailBean.RxInfoBean data);
    }

}